//
//  NotesList.m
//  NoteTakingApp
//
//  Created by anaraghi on 2/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NotesList.h"


@implementation NotesList

-(NSString*)getFilename {
	NSString *documentDirectoryPath = [Utilities documentDirectoryPath];
	return [documentDirectoryPath stringByAppendingPathComponent:@"NotesList.plist"];
}

-(NotesList*) init {
	self = [super init];
	if (self != nil) {
		
		NSLog(@"reading from %@", [self getFilename]);
		
		notes = [NSMutableArray arrayWithContentsOfFile:[self getFilename]];
		
		if (notes == nil) {
			notes = [NSMutableArray arrayWithCapacity:5];
		}	
	}
	[notes retain];
	return self;
}

-(void)writeToFile:(BOOL)atomic {
	[notes writeToFile:[self getFilename] atomically:atomic];
}

-(NSString*)getNoteAtIndex:(int)index {
	return [[notes objectAtIndex:index] objectForKey:@"contents"];
}

-(NSString*)getLabelAtIndex:(int)index {
	return [[notes objectAtIndex:index] objectForKey:@"label"];
}

-(void)newNote {
	NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithCapacity:2];
	NSString *newLabel = @"Untitled Note";
	[newDict setObject:newLabel forKey:@"label"];
	
	NSString *newNoteContents = @"Note contents go here.";
	[newDict setObject:newNoteContents forKey:@"contents"];
	[notes addObject:newDict];
}

-(void)setNoteLabel:(NSString *)newLabel atIndex:(int)index {
	[[notes objectAtIndex:index] setObject:newLabel forKey:@"label"];
	
	[self writeToFile:YES];
}

-(void)setNoteContents:(NSString *)noteContents atIndex:(int)index {
	[[notes objectAtIndex:index] setObject:noteContents forKey:@"contents"];
	
	[self writeToFile:YES];
}

-(int)size {
	return [notes count];
}

-(int)lastIndex {
	return [self size]-1;
}

-(void)deleteNoteAtIndex:(int)index {
	[notes removeObjectAtIndex:index];
}

@end
